package pl.puntin.punclient.messages;

/**
 * Zwykla wiadomosc tekstowa.
 * 
 * @author kakus
 *
 */
public final class ChatMessage extends Message
{
	public final String text;
	
	public ChatMessage(final String text)
	{
		this.text = text;
	}
	
	/**
	 * Konstruktor konwertujacy wiadomosc od serwera
	 * zgodnie z zalozeniem iz sklada sie on z
	 * 
	 * |  byte        |  reszta		  | <- rozmiar
	 * [typ_wiadmosci][teskt_wiadmosci] <- zawartosc
	 * 
	 * @param rawMessage wiadmosc otrzymna z sieci
	 */
	public ChatMessage(final byte[] rawMessage)
	{
		char[] messageText = new char[rawMessage.length-1];
		for (int i = 1; i < rawMessage.length; i++)
		{
			messageText[i-1]=(char)rawMessage[i];
		}
		text = new String(messageText);
	}

	@Override
	public byte getType()
	{
		return MessageTypes.CHAT_MESSAGE;
	}

	/* (non-Javadoc)
	 * @see pl.puntin.punclient.messages.Message#getByteForm()
	 * 
	 * UWAGA NIE TESTOWANE !
	 */
	@Override
	public byte[] getByteForm()
	{
		assert(false);
		byte[] byteForm = new byte[text.length()+1];
		for (int i = 0; i < text.length(); i++)
		{
			if(i == 0) byteForm[i] = getType();
			else
			{
				byteForm[i] = (byte) text.charAt(i-1);
			}
		}
		return byteForm;
	}
}
